package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.dto.CommunityResidentDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CommunityResident;
import com.ruoyi.system.service.ICommunityResidentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 住户管理Controller
 * 
 * @author ruoyi
 * @date 2024-03-27
 */
@RestController
@RequestMapping("/system/resident")
public class CommunityResidentController extends BaseController
{
    @Autowired
    private ICommunityResidentService communityResidentService;

    /**
     * 查询住户管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:resident:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunityResidentDto communityResidentDto)
    {
        startPage();
        List<CommunityResident> list = communityResidentService.selectCommunityResidentList(communityResidentDto);
        return getDataTable(list);
    }
    /**
     * 导入表格
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/import")

    public AjaxResult importFacilities(MultipartFile file) throws Exception {
        // 验证文件是否为空、格式是否正确等
        if (file.isEmpty() || !StringUtils.endsWithIgnoreCase(file.getOriginalFilename(), ".xlsx")) {
            return AjaxResult.error("请选择有效的 Excel (.xlsx) 文件进行导入");
        }

        // 调用服务层的导入方法，传入上传的文件
        int rowsAffected = communityResidentService.importCommunityResident(file);

        // 返回导入结果
        return rowsAffected > 0 ? AjaxResult.success("导入成功，共导入 " + rowsAffected + " 条数据")
                : AjaxResult.error("导入失败，请检查文件内容或联系管理员");
    }

    /**
     * 导出住户管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:resident:export')")
    @Log(title = "住户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunityResidentDto communityResidentDto)
    {
        List<CommunityResident> list = communityResidentService.selectCommunityResidentList(communityResidentDto);
        ExcelUtil<CommunityResident> util = new ExcelUtil<CommunityResident>(CommunityResident.class);
        util.exportExcel(response, list, "住户管理数据");
    }

    /**
     * 获取住户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:resident:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(communityResidentService.selectCommunityResidentById(id));
    }

    /**
     * 新增住户管理
     */
    @PreAuthorize("@ss.hasPermi('system:resident:add')")
    @Log(title = "住户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunityResident communityResident)
    {
        try {
            return toAjax(communityResidentService.insertCommunityResident(communityResident));
        } catch (Exception e){
            return AjaxResult.error("该住户已存在");
        }
    }

    /**
     * 修改住户管理
     */
    @PreAuthorize("@ss.hasPermi('system:resident:edit')")
    @Log(title = "住户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunityResident communityResident)
    {
        try{
            return toAjax(communityResidentService.updateCommunityResident(communityResident));
        } catch(Exception e){
            return AjaxResult.error("当前住户已经存在");
        }
    }

    /**
     * 删除住户管理
     */
    @PreAuthorize("@ss.hasPermi('system:resident:remove')")
    @Log(title = "住户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(communityResidentService.deleteCommunityResidentByIds(ids));
    }
}
